package com.leoren.run.activity.run;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.leoren.run.R;
import com.leoren.run.common.Const;
import com.leoren.run.vo.MapRouteVo;

import java.util.List;

/**
 * RunFragment 展示地图路径信息Adapter
 */
public class RunMapRouteAdapter extends RecyclerView.Adapter<RunMapRouteAdapter.RunMapItemHolder> {

    private Context context;
    private List<MapRouteVo> mapRouteVos;

    public RunMapRouteAdapter(Context context, List<MapRouteVo> mapRouteVos) {
        this.context = context;
        this.mapRouteVos = mapRouteVos;
    }

    static class RunMapItemHolder extends RecyclerView.ViewHolder{
        LinearLayout itemRunView;
        ImageView routeImg;
        TextView nameText;
        TextView timeText;
        TextView playerCountText;

        public RunMapItemHolder(View itemView){
            super(itemView);
            itemRunView = (LinearLayout) itemView;
            routeImg = itemView.findViewById(R.id.item_route_img);
            nameText = itemView.findViewById(R.id.item_route_name);
            timeText = itemView.findViewById(R.id.item_route_time);
            playerCountText = itemView.findViewById(R.id.item_route_player);
        }
    }

    @NonNull
    @Override
    public RunMapItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(context == null){
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.item_run_map_layout, parent, false);
        RunMapItemHolder holder = new RunMapItemHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RunMapItemHolder holder, int position) {
        final MapRouteVo vo = mapRouteVos.get(position);
        Glide.with(context).load(vo.getPicUrl()).into(holder.routeImg);
        holder.nameText.setText(vo.getName());
        holder.timeText.setText("大概用时" + vo.getTime() + "秒");
        holder.playerCountText.setText("已有" + vo.getPlayerCount() + "人参加");
        holder.itemRunView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, StartRunActivity.class);
                intent.putExtra(Const.GET_MAP_ID, vo.getId());
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return mapRouteVos.size();
    }
}
